package com.fly.monitor.console.service.impl;

import com.fly.monitor.console.service.LoggerService;
import com.fly.monitor.console.service.MonitorService;
import com.fly.monitor.console.task.RemoveTask;
import com.fly.monitor.core.enums.TraceKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.Fu
 * @date 2022/11/26 下午4:10
 */
@Service
public class MonitorServiceImpl implements MonitorService {

    private static final Logger log = LoggerFactory.getLogger(MonitorServiceImpl.class);

    @Resource
    private RemoveTask removeTask;
    @Resource
    private LoggerService loggerService;

    @Override
    public Map<String, Object> monitor(String request) {
        Map<String, Object> result = new HashMap<>(4);
        log.info("MonitorServiceImpl monitor request -> {}", request);
        result.put("data", getResponseData(request));
        result.put("traceId", MDC.get(TraceKey.TRACE_ID.getKey()));
        result.put("total", loggerService.save(request));
        removeTask.invoke(request);
        return result;
    }

    private String getResponseData(String request) {
        log.info("MonitorServiceImpl getResponseData request -> {}", request);
        return "monitor -> " + request;
    }
}
